package vg.com.chatonline.activity;

import kds.slm.SlidingMenu;
import kds.slm.app.SlidingFragmentActivity;
import vg.com.chatonline.adapter.DashboardAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kds.abs.app.ActionBar;
import com.kds.abs.app.ActionBar.Tab;
import com.vg.view.login_index;
import com.vg.view.user_left;

public class DashboardActivity extends SlidingFragmentActivity implements
		com.kds.abs.app.ActionBar.TabListener, OnPageChangeListener {

	private ViewPager pager;
	private Tab tab;
	kds.slm.app.SlidingFragmentActivity ac;
	SlidingMenu menu;
	Fragment mFrag;
	private SlidingMenu sm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		FragmentTransaction t = this.getSupportFragmentManager()
				.beginTransaction();
		mFrag = new user_left();
		t.replace(R.id.menu_frame, mFrag);
		t.commit();

		// customize the SlidingMenu
		sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);

		Toast.makeText(getApplicationContext(), "ma:" + login_index.id_static,
				Toast.LENGTH_SHORT).show();
		pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new DashboardAdapter(getSupportFragmentManager()));

		pager.setOnPageChangeListener(this);
		/**
		 * Tạo ActionBar cho tab,bao nhiêu tab thì add từng đó actionbar vào
		 * .SetTabListener : xác định vị trí của tab tương ứng với vị trí
		 * Fragment
		 * */
		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, null);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText("Tin nhắn");
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(R.drawable.icon_config_tab1);

		View tabIndicator1 = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, null);
		TextView title1 = (TextView) tabIndicator1.findViewById(R.id.title);
		title1.setText("Danh bạ");
		ImageView icon1 = (ImageView) tabIndicator1.findViewById(R.id.icon);
		icon1.setImageResource(R.drawable.icon_config_tab2);

		View tabIndicator2 = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, null);
		TextView title2 = (TextView) tabIndicator2.findViewById(R.id.title);
		title2.setText("Tìm bạn");
		ImageView icon2 = (ImageView) tabIndicator2.findViewById(R.id.icon);
		icon2.setImageResource(R.drawable.icon_config_tab3);

		View tabIndicator3 = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, null);
		TextView title3 = (TextView) tabIndicator3.findViewById(R.id.title);
		title3.setText("Profile");
		ImageView icon3 = (ImageView) tabIndicator3.findViewById(R.id.icon);
		icon3.setImageResource(R.drawable.icon_config_tab4);

		ActionBar actionBar = getSupportActionBar();

		// actionBar.setBackgroundDrawable(R.drawable.tab_indicator);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		tab = actionBar.newTab();
		tab.setCustomView(tabIndicator).setTabListener(this);
		actionBar.addTab(tab);

		actionBar.addTab(actionBar.newTab().setCustomView(tabIndicator1)
				.setTabListener(this));

		actionBar.addTab(actionBar.newTab().setCustomView(tabIndicator2)
				.setTabListener(this));

		actionBar.addTab(actionBar.newTab().setCustomView(tabIndicator3)
				.setTabListener(this));
		/**
		 * Trả về vị trí của TabBar
		 */
		actionBar.setSelectedNavigationItem(1);
		getSupportActionBar().setIcon(R.drawable.menu11);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		/**
		 * update trạng thái của ViewPager khi có sự thay đổi theo tab
		 * */
		pager.setCurrentItem(tab.getPosition());
		if (pager.getCurrentItem() == 0) {
			Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT)
					.show();
			sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		}
		pager.setCurrentItem(tab.getPosition());
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		/**
		 * Vị trí Page được chọn khi có sự thay đổi
		 * */
		getSupportActionBar().setSelectedNavigationItem(arg0);
		switch (arg0) {
		case 0:
			// on first page, you can get slidingmenu by swipping the entire
			// screen
			getSlidingMenu()
					.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			break;
		default:
			// on others page, you can get slidingmenu by swipping the half
			// screen
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
			break;
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onMenuItemSelected(int featureId,
			com.kds.abs.view.MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
		case android.R.id.home:
			if (sm != null) {
				sm.toggle();
			}
			break;
		}
		return true;
	}

}
